package SAXParser_Query;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ShoppingHandler extends DefaultHandler{
	
	NaverShopping naverShopping;
	
	private ArrayList<NaverShopping> shopping = new ArrayList<NaverShopping>();
	boolean isTitle = false;
	boolean isLink = false;
	boolean isLprice = false;
	

	public ArrayList<NaverShopping> getList() {
		return shopping;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equals("title")) {
			naverShopping = new NaverShopping();
			isTitle = true;
		} else if(qName.equals("link")) {
			isLink = true;
		} else if(qName.equals("lprice")) {
			isLprice = true;
		}
		
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		if(isTitle) {
			naverShopping.setTitle(new String(ch, start, length).trim());
		} else if(isLink) {
			naverShopping.setLink(new String(ch, start, length).trim());
		} else if(isLprice) {
			naverShopping.setLprice(new String(ch, start, length).trim());
		}

	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equals("title")) {
			isTitle = false;
		} else if(qName.equals("link")) {
			isLink = false;
		} else if(qName.equals("lprice")) {
			isLprice = false;
			shopping.add(naverShopping);
		}
	}
}
